package structure;

import java.util.ArrayList;

public class matrix {

	private ArrayList<position> matrix;
	
	public matrix(){
		
		this.matrix= new ArrayList<position>();
	}
	
	//get
	public ArrayList<String> get(int i, int j){
		
		for(position p : this.matrix){
			
			if(i == p.getI() && j == p.getJ()){
				
				return p.getS();
			}
		}
		
		return null;
	}
	
	public int getRowNumber(){
		
		int toReturn= 0;
		int next= 0, curr= 0;
		for(int k=0; k<this.matrix.size()-1; k++){
			
			curr= this.matrix.get(k).getI();
			next= this.matrix.get(k+1).getI();
			if(next != curr)
				toReturn++;
		}
		
		return toReturn;
	}
	
	public int getColNumber(){
		
		int toReturn= 0;
		int next= 0, curr= 0;
		for(int k=0; k<this.matrix.size()-1; k++){
			
			curr= this.matrix.get(k).getJ();
			next= this.matrix.get(k+1).getJ();
			if(next != curr)
				toReturn++;
		}
		
		return toReturn;
	}
	//
	
	//set
	public void add(int i, int j, String s){
		
		boolean added= false;
		for(position p : this.matrix){
			
			if(i == p.getI() && j == p.getJ()){
				
				p.add(s);
				added= true;
			}
		}
		
		if(!added){
			position p= new position(i, j, s);
			this.matrix.add(p);
		}
	}
	//
	
	public void printIt(){
		
		System.out.println("------------------------------------------------------------");
		System.out.println("RECOGNITION MATRIX");
		System.out.println("------------------------------------------------------------");
		//scorre le righe
		for(position p : this.matrix){ 
			p.printIt();
		}
		System.out.println("------------------------------------------------------------");
		
		//stampa la matrice in forma matriciale
		System.out.print("\t|  ");
		int prev= 0, cur= 0;
		for(int i=0; i<this.matrix.size(); i++){ 
			
			position tmpP= this.matrix.get(i);
			cur= tmpP.getI();
			if( prev != cur){
				
				prev= cur;
				System.out.println("");
				System.out.print("\t|  ");
			}
			tmpP.printItOrganized();
			
		}
		System.out.println("");
		System.out.println("------------------------------------------------------------");
		System.out.println("------------------------------------------------------------");
	}
}
